const g_iWndIndex = 0; //可以不用设置这个变量，有窗口参数的接口中，不用传值，开发包会默认使用当前选择窗口
const g_oLocalConfig = null; //本地配置

//错误码
//通用错误
const ERROR_CODE_UNKNOWN = 1000; //未知错误
const ERROR_CODE_NETWORKERROR = 1001; //网络错误
const ERROR_CODE_PARAMERROR = 1002; //缺少插件元素

//登录模块
const ERROR_CODE_LOGIN_NOLOGIN = 2000; // 未登录
const ERROR_CODE_LOGIN_REPEATLOGIN = 2001; //设备已登录，重复登录
const ERROR_CODE_LOGIN_NOSUPPORT = 2002; //当前设备不支持Digest登录

//预览播放
const ERROR_CODE_PLAY_PLUGININITFAIL = 3000; //插件初始化失败
const ERROR_CODE_PLAY_NOREPEATPLAY = 3001; //当前窗口已经在预览
const ERROR_CODE_PLAY_PLAYBACKABNORMAL = 3002; //回放异常
const ERROR_CODE_PLAY_PLAYBACKSTOP = 3003; //回放停止
const ERROR_CODE_PLAY_NOFREESPACE = 3004; //录像过程中，硬盘容量不足

//对讲
const ERROR_CODE_TALK_FAIL = 5000; //语音对讲失败

const ip = "172.16.0.241";
const port = "80",
  username = "admin",
  password = "Aa123456";

const szDeviceIdentify = ip + "_" + port;

// 初始化插件
const initPlugin = (
  ip,
  port,
  username,
  password,
  channelID,
  divId = "divPlugin"
) => {
  WebVideoCtrl.I_InitPlugin({
    bWndFull: true,
    iWndowType: 1,
    cbSelWnd: function (xmlDoc) {},
    cbDoubleClickWnd: function (iWndIndex, bFullScreen) {},
    cbEvent: function (iEventType, iParam1, iParam2) {
      if (2 == iEventType) {
        // 回放正常结束
        console.log("窗口" + iParam1 + "回放结束！");
      } else if (-1 == iEventType) {
        console.log("设备" + iParam1 + "网络错误！");
      } else if (3001 == iEventType) {
      }
    },
    cbInitPluginComplete: function () {
      WebVideoCtrl.I_InsertOBJECTPlugin(divId).then(
        () => {
          // 检查插件是否最新
          WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
            if (bFlag) {
              alert(
                "检测到新的插件版本，双击开发包目录里的HCWebSDKPlugin.exe升级！"
              );
            } else {
              console.log("海康威视插件初始化成功！");
              loginDevice(channelID);
            }
          });
        },
        () => {
          alert(
            "插件初始化失败，请确认是否已安装插件；如果未安装，请双击开发包目录里的HCWebSDKPlugin.exe安装！"
          );
        }
      );
    },
  });
};

// 登录
const loginDevice = (channelID) => {
  console.log("开始登录设备。。。");

  WebVideoCtrl.I_Login(ip, 1, port, username, password, {
    timeout: 3000,
    success: function (xmlDoc) {
      console.log(szDeviceIdentify + " 登录成功！");
      //   getDevicePort();

      setTimeout(() => {
        console.log("开始预览。。。");
        startRealPlay(channelID);
      }, 300);
    },
    error: function (oError) {
      if (ERROR_CODE_LOGIN_REPEATLOGIN === status) {
        console.log("已经登录过！");
      } else {
        console.log(oError);
      }
    },
  });
};

// 获取设备端口
function getDevicePort() {
  if (null == szDeviceIdentify) {
    return;
  }

  WebVideoCtrl.I_GetDevicePort(szDeviceIdentify).then(
    (oPort) => {
      console.log(szDeviceIdentify + " 获取端口成功！" + oPort);
    },
    (oError) => {
      var szInfo = "获取端口失败！";
      console.log(szDeviceIdentify + szInfo, oError.errorCode, oError.errorMsg);
    }
  );
}

// 退出
function logout() {
  if (null == szDeviceIdentify) {
    return;
  }

  WebVideoCtrl.I_Logout(szDeviceIdentify).then(
    () => {
      console.log(szDeviceIdentify + " " + "退出成功！");
    },
    () => {
      console.log(szDeviceIdentify + " " + "退出失败！");
    }
  );
}

// 预览
const startRealPlay = (channelID) => {
  console.log("获取到的 ID", channelID);
  var oWndInfo = WebVideoCtrl.I_GetWindowStatus(g_iWndIndex);

  var startRealPlay = function () {
    WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
      iStreamType: 1,
      iChannelID: channelID,
      bZeroChannel: false,
      success: function () {
        console.log(`设备标识：${szDeviceIdentify}`);
      },
      error: function (oError) {
        console.log(
          szDeviceIdentify + " 开始预览失败！",
          oError.errorCode,
          oError.errorMsg
        );
      },
    });
  };

  if (oWndInfo != null) {
    // 已经在播放了，先停止
    WebVideoCtrl.I_Stop({
      success: function () {
        startRealPlay();
      },
    });
  } else {
    startRealPlay();
  }
};

export { initPlugin, loginDevice };
